﻿/*
Copyright (c) 2011, Pawel Szczurek
All rights reserved.


Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:


Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or
other materials provided with the distribution.

Neither the name of the <ORGANIZATION> nor the names of its contributors may be used to endorse or promote products derived from this software without
specific prior written permission.


THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;

namespace Alice.Script
{
    /// <summary>
    /// program that can be executed
    /// </summary>
    public class Program
    {
        public static readonly int Version = 100;

        public Program() { }
        public Program(IList<byte> code, IList<ConstValue> constValue)
        {
            //copy program
            m_programCode = new byte[code.Count];
            for (int i = 0; i < code.Count; i++)
                m_programCode[i] = code[i];

            //copy const
            m_constantValues = new ConstValue[constValue.Count];
            for (int i = 0; i < constValue.Count; i++)
                m_constantValues[i] = new ConstValue(constValue[i]);
        }

        public class ConstValue
        {
            public enum Type : byte
            {
                StringType,
                NumericType
            }

            public ConstValue(string v)
            {
                m_type = Type.StringType;
                StringValue = v;
            }
            public ConstValue(double v)
            {
                m_type = Type.NumericType;
                NumericValue = v;
            }
            public ConstValue(ConstValue cv)
            {
                NumericValue = cv.NumericValue;
                StringValue = cv.StringValue;
                m_type = cv.m_type;
            }

            public void Serialize(BinaryWriter outStream)
            {
                outStream.Write((byte)m_type);
                if (m_type == Type.NumericType)
                    outStream.Write(NumericValue);
                else
                    outStream.Write(StringValue);
            }
            public void Deserialize(BinaryReader inStream)
            {
                m_type = (Type)inStream.ReadByte();
                if (m_type == Type.NumericType)
                    NumericValue = inStream.ReadDouble();
                else
                    StringValue = inStream.ReadString();
            }

            public Type GetValueType()
            {
                return m_type;
            }

            public string StringValue;
            public double NumericValue;

            public override string ToString()
            {
                if (m_type == Type.NumericType)
                    return string.Format("[Numeric]:[{0}]", NumericValue);
                else
                    return string.Format("[String ]:[{0}]", StringValue.Replace("\n", "\\n"));
            }

            #region private

            Type m_type;

            #endregion
        }

        public void Serialize(BinaryWriter outStream)
        {
            //save version
            outStream.Write(Version);

            //save program
            outStream.Write(m_programCode.Length);
            outStream.Write(m_programCode);

            //save consts
            outStream.Write(m_constantValues.Length);
            for (int i = 0; i < m_constantValues.Length; i++)
                m_constantValues[i].Serialize(outStream);
        }
        public void Deserialize(BinaryReader inStream)
        {
            //check version
            int v = inStream.ReadInt32();
            Debug.Assert(v == Version);

            //load program
            int porgramLen = inStream.ReadInt32();
            m_programCode = inStream.ReadBytes(porgramLen);

            //load consts
            int constLen = inStream.ReadInt32();
            m_constantValues = new ConstValue[constLen];
            for (int i = 0; i < m_constantValues.Length; i++)
            {
                m_constantValues[i] = new ConstValue(0);
                m_constantValues[i].Deserialize(inStream);
            }
        }

        public byte[] ProgramCode
        {
            get { return m_programCode; }
        }
        public ConstValue[] ConstantValues
        {
            get { return m_constantValues; }
        }

        #region private

        byte[] m_programCode;
        ConstValue[] m_constantValues;

        #endregion
    }
}
